#! /usr/bin/env python
# coding=utf-8

import sys
import argparse
from argparse import RawTextHelpFormatter


parser = argparse.ArgumentParser(
    description='''
    对TRIP 端粒搜索 进行过滤 去除 4个以下 且纯粹由短重复构成的重复
    用法:
    TRIP_repeat_filter.py -i Ppup_out.tsv -o Ppup_repeatsummary.tsv
    ''',formatter_class=RawTextHelpFormatter)

parser.add_argument('-i',
                help='输入 RepeatSummary_v2的输出文件')

parser.add_argument('-o',
                help='输出')

args = parser.parse_args()

if not args.i or not args.o:

    parser.print_help()

    sys.exit()


infile = args.i 

outfile = args.o

tmp_lista = []

less_than_four = []

with open(infile) as fila:
	for i in fila:
		k = i.strip().split('\t')
		#seq = k[0]
		if len(k)>1:
			if len(k[0])>3:
				tmp_lista.append(k)
				#less_than_four.append(k[0])
			else:
				less_than_four.append(k[0])

filtered_lista = []

n = 0

for i in tmp_lista:
	t = 0
	n += 1
	if n%100000 ==0:
		print(n)
	for j in less_than_four:
		if len(j) < len(i[0]):
			if i[0].replace(j,'') =='':
				t = 1
				break
	if t != 1:
		filtered_lista.append(i)


with open(outfile,'w') as fila:
	for i in filtered_lista:
		fila.write('\t'.join(i)+'\n')
#for s in less_than_four:
#	print(s)
#	break